package com.wonder.config;

import org.eclipse.paho.client.mqttv3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * 生产者客户端消息回调
 */
@Component
public class MqttProviderCallBack implements MqttCallback {
    @Autowired
    private MqttProviderConfig mqttProviderConfig;
    /**
     * 客户端断开连接的回调
     * @param throwable
     */
    @Override
    public void connectionLost(Throwable throwable) {
        System.out.println("发送方与服务器断开连接，可重连");
        mqttProviderConfig.connect();
        System.out.println("发送方重连成功");
    }

    /**
     * 消息到达的回调
     * @param topic
     * @param message
     * @throws Exception
     */
    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        System.out.println(String.format("发送消息主题 : %s",topic));
        System.out.println(String.format("发送消息Qos : %d",message.getQos()));
        System.out.println(String.format("发送消息内容 : %s",new String(message.getPayload())));
        System.out.println(String.format("发送消息retained : %b",message.isRetained()));
    }

    /**
     * 消息发布成功的回调
     */
    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

        System.out.println(String.format("发送消息成功"));

    }
}
